FROM php:7.3-fpm-stretch

COPY sources.list /etc/apt/

# install the PHP extensions we need (https://make.wordpress.org/hosting/handbook/handbook/server-environment/#php-extensions)
RUN set -ex; \
	\
	savedAptMark="$(apt-mark showmanual)"; \
	\
	cp "/usr/share/zoneinfo/Asia/Shanghai" "/etc/localtime" && \
        echo "Asia/Shanghai" > "/etc/timezone" && \
	apt-get update && apt-get install -y --no-install-recommends \
		nginx \
		libjpeg-dev \
		libicu-dev \
		libxml2-dev \
        libxml2 \
        libxslt-dev \
        libfreetype6-dev \
        libjpeg \
        libpng-dev \
        libmagickwand-dev \
        zlib1g-dev \
        libzip-dev \
        libmemcached-dev \
        libmcrypt-dev \
	; \
	\
	docker-php-ext-configure gd \
        --with-freetype-dir=/usr/include/ \
        --with-png-dir=/usr/include/ \
        --with-jpeg-dir=/usr/include/ ; \
	docker-php-ext-install -j$(nproc) \
		bcmath \
		exif \
		gd \
		mysqli \
		opcache \
		zip \
		intl \
		pdo_mysql \
		dom \
        xmlrpc \
        xsl \
        soap \
        fileinfo \
	; \
	pecl install \
        imagick-3.4.3 \
        memcached-3.1.5 \
        redis-3.1.3 \
        swoole-4.4.14 \
        mcrypt-1.0.3 \
    ; \
	docker-php-ext-enable \
        imagick \
        memcached \
        redis \
        swoole \
        mcrypt \
    ; \
	\
# reset apt-mark's "manual" list so that "purge --auto-remove" will remove all build dependencies
	apt-mark auto '.*' > /dev/null; \
	apt-mark manual $savedAptMark; \
	apt-mark manual nginx; \
	ldd "$(php -r 'echo ini_get("extension_dir");')"/*.so \
		| awk '/=>/ { print $3 }' \
		| sort -u \
		| xargs -r dpkg-query -S \
		| cut -d: -f1 \
		| sort -u \
		| xargs -rt apt-mark manual; \
	\
	apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
	apt-get clean ; \
	rm -rf /var/lib/apt/lists/*;

# set recommended PHP.ini settings
# see https://secure.php.net/manual/en/opcache.installation.php
RUN { \
		echo 'opcache.memory_consumption=128'; \
		echo 'opcache.interned_strings_buffer=8'; \
		echo 'opcache.max_accelerated_files=4000'; \
		echo 'opcache.revalidate_freq=2'; \
		echo 'opcache.fast_shutdown=1'; \
		echo 'opcache.enable_cli=1'; \
	} > /usr/local/etc/php/conf.d/opcache-recommended.ini
# https://codex.wordpress.org/Editing_wp-config.php#Configure_Error_Logging
RUN { \
		echo 'error_reporting = E_ALL'; \
		echo 'display_errors = Off'; \
		echo 'display_startup_errors = Off'; \
		echo 'log_errors = On'; \
		echo 'error_log = /dev/stderr'; \
		echo 'log_errors_max_len = 1024'; \
		echo 'ignore_repeated_errors = On'; \
		echo 'ignore_repeated_source = Off'; \
		echo 'html_errors = Off'; \
		echo 'post_max_size = 20M'; \
		echo 'upload_max_filesize = 20M'; \
	} > /usr/local/etc/php/conf.d/error-logging.ini

COPY nginx.conf /etc/nginx/
COPY default.conf /etc/nginx/conf.d/
COPY entrypoint.sh /usr/local/bin/
COPY zz-docker.conf /usr/local/etc/php-fpm.d/
COPY composer /usr/local/bin/

VOLUME /var/www/html

ENTRYPOINT ["entrypoint.sh"]
CMD ["php-fpm"]
